import { getRequest } from 'components/functions';
import { CarInfoModel, CarMessageModel, ImgsModel, PageData } from 'pages/kungfu/model/ReportData';
import { TableValue } from 'pages/kungfu/model/TableValue';
import { CertData } from 'pages/kungfu/model/CertData';
import { Ref } from 'vue';


interface ReportData {
  info: object,
  img: ResImg[],
  status: ResStatus
}

interface ResImg {
  key: number,
  name: string,
  "object": string
}

interface ResStatus {
  "is_wading": number,
  "is_fire": number,
  "is_steel7": number,
  "is_silver4": number
}

function fetchData (uuid:string,analyze: (data: ReportData) => void) {
  getRequest('/detection/detection/CReport?uuid='+ uuid,(resData: {status_code:number,msg:string,body:ReportData}) => {
    const body:ReportData = resData.body;
    analyze(body)
  })
  // analyze(testData.body)
}


export function getReportData (uuid:string, pageData:Ref<PageData>): void {
  fetchData(uuid,(resData:ReportData) => {
    const body = resData
    const info = body.info;
    const status = body.status;
    let carInfoDataSet = [];
    let carMessageDataSet = [];

    for (var val in info) {

      if (
        val == 'structure'
        || val == 'guidance_price'
        || val == 'car_body_color'
        || val == 'fuel_type'
      ) {
        carInfoDataSet.push({
          label: info[val].name,
          value: info[val].content
        });
      }

      if (
        val == 'vin'
        || val == 'registration_date'
        || val == 'mileage'
      ) {
        carMessageDataSet.push({
          label: info[val].name,
          value: info[val].content
        });
      }
    }

    let carInfo: CarInfoModel = {
      // @ts-ignore
      carType: info.car_name.content,
      dataSet: carInfoDataSet,
      // @ts-ignore
      standardL: info.environment.content
    }
    let urls = []
    const img = body.img
    let imgs: ImgsModel = null;
    if  ( img !== undefined && img.length > 0) {
      img.forEach((item:ResImg) => {
        const url = item.object;
        urls.push(url)
      })
       imgs = {
        // @ts-ignore
        reportDate: info.finished_at.content,
        imgs: urls
      }
    }



    let carMessage: CarMessageModel = {
      dataSet: carMessageDataSet,
      quality: status.is_steel7 == 0 && status.is_silver4 == 0,
      water: status.is_wading != 1,
      fire: status.is_fire != 1,
      crash: status.is_steel7 != 1,
      sliver: status.is_silver4 != 1
    }
    pageData.value = {
      carInfoData: carInfo,
      imgData: imgs,
      carMessageData: carMessage
    }
  })
}


export function getCertData (uuid:string, certData:Ref<CertData>): void {
  fetchData(uuid,(resData: ReportData) => {
    const info = resData.info;
    certData.value = {
      no: info['report_no'] !== undefined ? info['report_no'].content : '',
      carType: info['car_name'] !== undefined ? info['car_name'].content : '',
      gearBox: info['gearbox'] !== undefined ? info['gearbox'].content : '',
      miles: info['mileage'] !== undefined ? info['mileage'].content : '',
      swept: info['exhaust'] !== undefined ? info['exhaust'].content : '',
      registerDate: info['registration_date'] !== undefined ? info['registration_date'].content : '',
      fuelType: info['fuel_type'] !== undefined ? info['fuel_type'].content : '',
      vinCode: info['vin'] !== undefined ? info['vin'].content : '',
      channelName: info['dealer_name'] !== undefined ? info['dealer_name'].content : '',
      tester: info['tester_fullname'] !== undefined ? info['tester_fullname'].content : '',
      finishDate: info['finished_at'] !== undefined ? info['finished_at'].content : ''
    }

  })


}




const testData:{status_code:number,msg:string,body:ReportData} = {
  "status_code": 1,
  "msg": "操作成功", //返回文字描述
  "body": {
    "info": {
      "report_no": {
        "key": "report_no",
        "name": "证书编码",
        "content": "0000000616"
      },
      "structure": {
        "key": "structure",
        "name": "车身结构",
        "content": "三厢车"
      },
      "tester_fullname": {
        "key": "tester_name",
        "name": "检测师",
        "content": "胡卫"
      },
      "finished_at": {
        "key": "finished_at",
        "name": "认证日期",
        "content": "2021年10月23日"
      },
      "fuel_type": {
        "key": "fuel_type",
        "name": "能源类型",
        "content": "-"
      },
      "gearbox": {
        "key": "gearbox",
        "name": "变速箱类型",
        "content": "7挡干式双离合"
      },
      "guidance_price": {
        "key": "guidance_price",
        "name": "新车指导价",
        "content": "26.88万"
      },
      "environment": {
        "key": "environment",
        "name": "环保标准",
        "content": "国V+OBD"
      },
      "dealer_name": {
        "key": "dealer_name",
        "name": "经销商名称",
        "content": "迈卡经销商测试4"
      },
      "market_name": {
        "key": "market_name",
        "name": "市场名称",
        "content": "迈卡交易市场"
      },
      "car_name": {
        "key": "car_name",
        "name": "车辆名称",
        "content": "大众/一汽-大众CC/一汽-大众CC 2016款 1.8TSI 豪华型"
      },
      "vin": {
        "key": "vin",
        "name": "车架号",
        "content": "LFV3A23C8G3404824"
      },
      "exhaust": {
        "key": "exhaust",
        "name": "排量",
        "content": "1.8"
      },
      "is_booster": {
        "key": "is_booster",
        "name": "是否增压",
        "content": "否"
      },
      "car_body_color": {
        "key": "car_body_color",
        "name": "车身颜色",
        "content": "-"
      },
      "registration_date": {
        "key": "registration_date",
        "name": "注册日期",
        "content": ""
      },
      "mileage": {
        "key": "mileage",
        "name": "表现里程",
        "content": "7.74万公里"
      }
    },
    "img": [
      {
        "key": 1,
        "name": "左前45度",
        "object": "https://produce-bucket.obs.cn-east-3.myhuaweicloud.com:443/kungfu/auto/487caf820c83e1b5aef912ce74fff4e1.jpg?x-obs-security-token=gQljbi1lYXN0LTOIYflOlQdZu0aAHmxAnXJVmJC2ZtURVei8Jw2PrVvMXHbIPXR5pG5x5k5FRIRJjyuRMG-QcjT5606vTu-yiFZHw-QMEsndq_YSOcswqFcJ0rHGDwu_WSsEMdA9GSnq_3aKwz9cGBIy9AcIR336CNwk4MPjKWCo4kWmuraHe6MnQVh_kgXyHmp2gJxVMiu_p0sGnRbn931M4sPz7ANWjuVGyUD8yvEnpsviW1j_cctQuQI60p0Fz9-GIxS6r3SCxVk1xdGXV8QPDRY-kFtB6XA09fDbi58CVs7Wj6sgaPCKfYDW33GY1IudGdWN1qLf_Ggh8beSoPIMnwUjbXffCVb39dMHzzzpmgaXI1smCzsLgv2TBu-zH9yN4o2MTfnpRDMwtvKIrS4WdXm2Xww_QiIhaMXnvstR7hKEgKOc5S8sA-Czeu6U1pcfUkBcVVOCEwoueBoLD80sX8YrNi5wa5VeGmUdRuIpL4RY0VDudlRueBIdQHEOEbFQ5COw_eHEnnu7q7UpA1nOGxkmuAZkBXPHAEJi6qq_EHsrAZlWVrb9lLsIA-NRZWCzh2mKUCX61QmwW3JQiAVInV4N1pBjpDIhva1Umhhtg3bjUr4avak3PKgwkZDb83-yTdAK1xtaApUhH8xln6MjY5TKAlVlu4rcZd2P3k1NK91Aa9jCf2Vxr7gouW1nGIccSEdB3PBhkL4LO8mKtLu69ImDc2d9Sudm8gn45WX-M5doPnWz4Ij4czkfiTqz2_4DjyGZFrFqIeE2SNf6HO7qvZ0LFToZpkGuK84h0Wso_fexbGAm9q3W4JQ%3D&AccessKeyId=8UAC1WT9OEVDZQO3B3WX&Expires=1636698875&Signature=myW4H2evmv806u9NegtpPgSZvSY%3D"
      }
    ],
    "status": {
      "is_wading": 1, //是否水泡 -1 未评定（未得出结果） ， 0 不是 1 是
      "is_fire": 0, //是否火烧  -1 未评定（未得出结果） ， 0 不是 1 是
      "is_steel7": 0, //是否事故 -1 未评定（未得出结果） ， 0 不是 1 是
      "is_silver4": 1 //是否银四 -1 未评定（未得出结果） ， 0 不是 1 是
    }
  }
}
